package com.xinchuang.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xinchuang.domain.pojo.TestNoticeTime;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2023/9/26 11:56
 */
public class NoticeTimeUtil {

    private static DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    public static List<TestNoticeTime> getTimes(List<TestNoticeTime> times, JSONArray array, int type, String id) {
       // List<LinkedHashMap> maps = array.toJavaList(LinkedHashMap.class);
        List<LinkedHashMap<String, Object>> maps = JSON.parseObject(array.toJSONString(), new TypeReference<List<LinkedHashMap<String, Object>>>() {});
        String startTime = "";
        String endTime = "";
        String seance = "";
        String end = "";
        switch (type) {
            case 1:
                startTime = "loadStarttime";
                endTime = "loadEndttime";
                seance = "senceload";
                end = "endload";
                break;
            case 2:
                startTime = "waitStarttime";
                endTime = "waitEndtime";
                seance = "senceawait";
                end = "endawait";
                break;
            case 3:
                startTime = "demoProduceStarttime";
                endTime = "demoProduceEndtime";
                seance = "sencedemoProduce";
                end = "enddemoProduce";
                break;
            case 4:
                startTime = "unloadStarttime";
                endTime = "unloadEndtime";
                seance = "senceunload";
                end = "endunload";
                break;
            case 5:
                startTime = "breakdownStarttime";
                endTime = "breakdownEndtime";
                seance = "sencebreakdown";
                end = "endbreakdown";
                break;
            case 6:
                startTime = "fixStarttime";
                endTime = "fixEndtime";
                seance = "senceDiebreak";
                end = "endDiebreak";
                break;
            case 7:
                startTime = "debugStarttime";
                endTime = "debugEndTime";
                seance = "encedebug";
                end = "enddebug";
                break;
            default:
                break;
        }
        int count = 1;
        for (LinkedHashMap map : maps) {
            TestNoticeTime noticeTime = new TestNoticeTime();
            noticeTime.setParentNoticeId(id);
            if (ObjectUtil.isNotEmpty(map.get(startTime))) {
                noticeTime.setStartTime(LocalDateTime.parse(map.get(startTime).toString(), sdf));
            }
            if (ObjectUtil.isNotEmpty(map.get(endTime))) {
                noticeTime.setEndTime(LocalDateTime.parse(map.get(endTime).toString(), sdf));
            }
            if (ObjectUtil.isNotEmpty(noticeTime.getStartTime()) && ObjectUtil.isNotEmpty(noticeTime.getEndTime())
                    && noticeTime.getStartTime().compareTo(noticeTime.getEndTime()) >= 0) {
                throw new RuntimeException();
            }
            noticeTime.setIntervals(getTimeByLong(noticeTime.getStartTime(), noticeTime.getEndTime()));
            noticeTime.setSeance(map.get(seance).toString());
            noticeTime.setEnd(map.get(end).toString());
            noticeTime.setSort(count);
            noticeTime.setStatus(type);
            times.add(noticeTime);
            count++;
        }
        return times;
    }

    public static JSONArray getTimeArray(List<TestNoticeTime> list, int type) {
        JSONArray array = new JSONArray();
        String startTime = "";
        String endTime = "";
        String time = "";
        String seance = "";
        String end = "";
        switch (type) {
            case 1:
                startTime = "loadStarttime";
                endTime = "loadEndttime";
                time = "loadTime";
                seance = "senceload";
                end = "endload";
                break;
            case 2:
                startTime = "waitStarttime";
                endTime = "waitEndtime";
                time = "waitTime";
                seance = "senceawait";
                end = "endawait";
                break;
            case 3:
                startTime = "demoProduceStarttime";
                endTime = "demoProduceEndtime";
                time = "demoProduceTime";
                seance = "sencedemoProduce";
                end = "enddemoProduce";
                break;
            case 4:
                startTime = "unloadStarttime";
                endTime = "unloadEndtime";
                time = "unloadTime";
                seance = "senceunload";
                end = "endunload";
                break;
            case 5:
                startTime = "breakdownStarttime";
                endTime = "breakdownEndtime";
                time = "breakDownTime";
                seance = "sencebreakdown";
                end = "endbreakdown";
                break;
            case 6:
                startTime = "fixStarttime";
                endTime = "fixEndtime";
                time = "fixTime";
                seance = "senceDiebreak";
                end = "endDiebreak";
                break;
            case 7:
                startTime = "debugStarttime";
                endTime = "debugEndTime";
                time = "debugTime";
                seance = "encedebug";
                end = "enddebug";
                break;
            default:
                break;
        }
        if (CollectionUtil.isNotEmpty(list)) {
            for (TestNoticeTime noticeTime : list) {
                JSONObject json = new JSONObject();
                json.put(startTime, ObjectUtil.isNotEmpty(noticeTime.getStartTime()) ? sdf.format(noticeTime.getStartTime()) : "");
                json.put(endTime, ObjectUtil.isNotEmpty(noticeTime.getEndTime()) ? sdf.format(noticeTime.getEndTime()) : "");
                json.put(time, getTime(noticeTime.getIntervals()));
                json.put(seance, Boolean.getBoolean(noticeTime.getSeance()));
                json.put(end, Boolean.getBoolean(noticeTime.getEnd()));
                array.add(json);
            }
        } else {
            JSONObject json = new JSONObject();
            json.put(startTime, "");
            json.put(endTime, "");
            json.put(time, 0);
            json.put(seance, false);
            json.put(end, false);
            array.add(json);
        }
        return array;
    }

    public static Long getTimeByLong(LocalDateTime startTime, LocalDateTime endTime) {
        if (ObjectUtil.isNotEmpty(startTime) && ObjectUtil.isNotEmpty(endTime)) {
            Duration duration = Duration.between(startTime, endTime);
            return duration.toMillis();
        }
        return 0L;
    }

    public static String getTime(long time) {
        if (time == 0L) {
            return "无";
        }
//        else if (time < 60 * 1000) {
//            return (time / 1000) + "秒";
//        } else if (time >= 60 * 1000 && time <= 60 * 60 * 1000) {
//            return MyUtils.formatDouble(time / 1000 / 60.0) + "分钟";
//        }
        else {
            return MyUtils.formatDouble(time / 1000 / 60.0 / 60.0) + "小时";
        }
    }

}
